package com.ai.networking.IXCom;


import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.Socket;

@Slf4j
public class TongXinThreadTangent extends Thread {

    private Socket socket;
    private BufferedReader in;
    private OutputStream out;

    public TongXinThreadTangent(Socket socket) {
        this.socket = socket;

    }

    public void send(String msg) {
        try {
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
            printWriter.write(msg);
            printWriter.flush();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public void sendAll(String msg) {
        synchronized (IXComServerTangent.list) {
            for (TongXinThreadTangent t : IXComServerTangent.list) {
                t.send(msg);
                log.info("服务端返回：" + msg);
            }
        }
    }

    @Override
    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
            out = socket.getOutputStream();

            String result = "";
            while (!StringUtils.isEmpty(result = in.readLine())) {
                log.info("L3 服务端接收消息：" + result);
                String appCode = result.substring(4, 10);
                if ("999999".equals(appCode)) {
                    log.info("L3 服务端接收消息为心跳信息");
                } else {

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void handleMsg(String result) {

        try {

        } catch (Exception e) {
            log.error("数据解析错误", e);
            e.printStackTrace();
        }
    }
}

